<?php
/**
 * Created by PhpStorm.
 * User: dong
 * Date: 2018/4/9
 * Time: 21:53
 */
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\BankTraits\RYFBankMap;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class RYFPayment extends RechargeBaseAbstract implements WAPable
{
    use RYFBankMap;

    protected $gw_pay = 'https://gateway.ruyipay.com/Pay/KDBank.aspx';
    protected $gw_query = 'https://gateway.ruyipay.com/Pay/Query.aspx';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['P_ErrCode'] == 0) {
                return true;
            }
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $signStr = "";
        $signStr .= $data['P_UserId'] . '|';
        $signStr .= $data['P_OrderId'] . '|';
        $signStr .= $data['P_CardId'] . '|';
        $signStr .= $data['P_CardPass'] . '|';
        $signStr .= $data['P_FaceValue'] . '|';
        $signStr .= $data['P_ChannelId'] . '|';
        $signStr .= $data['P_PayMoney'] . '|';
        $signStr .= $data['P_ErrCode'] . '|';
        $signStr .= $this->getMchKey();
        if (strcasecmp($data['P_PostKey'], md5($signStr)) == 0) return true;
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['P_OrderId'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['P_FaceValue'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::RUYIFU;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::BANK    => '1',
            PaymentMap::ALI    => '2',
            PaymentMap::WX    => '21',
            PaymentMap::BANK_WAP    => '32',
            PaymentMap::WX_WAP    => '33',
            PaymentMap::ALI_WAP    => '36',
            PaymentMap::QQ      => '89',
            PaymentMap::QQ_WAP      => '92',
            PaymentMap::JD          => '91'
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('P_UserID', $this->getMchId());
        $this->setParameter('P_OrderID', $rechargeOrder->plat_no);
        $this->setParameter('P_CardID', '');
        $this->setParameter('P_FaceValue', $rechargeOrder->order_amt);
        $this->setParameter('P_ChannelID', $this->getPaymentMap($rechargeOrder->order_data['recharge_type']));
        $this->setParameter('P_PostKey', $this->querySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters());
        if ($res['http_code'] == 200) {
            try {
                parse_str($res['body'],$output);
                if ($output['P_flag'] == 1 && $output['P_status'] == 1) {
                    return true;
                }
            } catch (\Exception $exception) {
                return $res['body'];
            }
        }
        return $res['body'];
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() . '?' . http_build_query($this->getParameters());
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        $url = $this->getGwPay() . '?' . http_build_query($this->getParameters());
        return $url;
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $signStr = "";
        $signStr .= $this->getParameter('P_UserID') . '|';
        $signStr .= $this->getParameter('P_OrderID') . '|';
        $signStr .= $this->getParameter('P_CardID') . '|';
        $signStr .= $this->getParameter('P_CardPass') . '|';
        $signStr .= $this->getParameter('P_FaceValue') . '|';
        $signStr .= $this->getParameter('P_ChannelID') . '|';
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr = "";
        $signStr .= "P_UserId=" . $this->getParameter('P_UserID') . '&';
        $signStr .= "P_OrderId=" . $this->getParameter('P_OrderID') . '&';
        $signStr .= "P_ChannelId=" . $this->getParameter('P_ChannelID') . '&';
        $signStr .= "P_CardId=" . $this->getParameter('P_CardID') . '&';
        $signStr .= "P_FaceValue=" . $this->getParameter('P_FaceValue') . '&';
        $signStr .= "P_PostKey=" . $this->getMchKey();
        return md5($signStr);
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'ErrCode=0';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        $mchData = $rechargeOrder->order_data;
        // TODO: Implement initParameters() method.
        $this->setParameter('P_UserID', $this->getMchId());
        $this->setParameter('P_OrderID', $rechargeOrder->plat_no);
        $this->setParameter('P_CardID', '');
        $this->setParameter('P_CardPass', '');
        $this->setParameter('P_FaceValue', $rechargeOrder->order_amt);
        $this->setParameter('P_Price', $rechargeOrder->order_amt);
        $this->setParameter('P_ChannelID', $this->getPaymentMap($mchData['recharge_type']));
        $this->setParameter('P_Subject', 'aiispay-order');
        if (PaymentMap::isBankHref($mchData['recharge_type'])) {
            $this->setParameter('P_Description', $this->getBank($mchData['bank_code']));
        }
        $this->setParameter('P_Notic', $this->setRemark($rechargeOrder));
        $this->setParameter('P_Result_URL', $this->getCallbackUrl());
        $this->setParameter('P_Notify_URL', $this->getReturnUrl());
        $this->setParameter('P_PostKey', $this->paySign());

    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        return new HrefRedirectAction($this->getGwPay() . '?' . http_build_query($this->getParameters()));
    }
}